CREATE OR REPLACE PACKAGE BODY EDIV2.Pack_?Categoria_Flotte AS
/******************************************************************************
   NAME:       Pack_?Categoria_Flotte
   PURPOSE:

   REVISIONS:
   Ver        Date        Author           Description
   ---------  ----------  ---------------  ------------------------------------
   1.0        21/3/2014   lLara       Creacion del Paquete
******************************************************************************/

PROCEDURE obd.FlotteInsertarModificar(@pprotocol VARCHAR(30), @pidSystem INT, @pecuHardware VARCHAR(30), 
								@pecuType VARCHAR(30), @pengineProject VARCHAR(30), @pdisplacement NUMERIC, 
								@pemissionConcept VARCHAR(30), @pplatform VARCHAR(30), @ppower VARCHAR(30), 
								@pgearbox VARCHAR(30), @pvehicle VARCHAR(30), @pmanufacturer VARCHAR(30), 
								 pTransaccion OUT NUMBER, pMensaje OUT VARCHAR2)
AS
vExisteRegistro NUMBER(1);
BEGIN
  SELECT count(*)
  INTO   vExisteRegistro
  FROM   obd.Flotte
  WHERE  nidDocumento=pNidDocumento;
  
  IF vExisteRegistro=0 THEN
    INSERT INTO obd.Flotte
	           (idFlotte, protocol, idSystem, 
				ecuHardware, ecuType, engineProject, 
				displacement, emissionConcept, platform, 
				power, gearbox, vehicle, 
				manufacturer)
    VALUES     (seq_Flotte.nextval, @pprotocol, @pidSystem, 
				@pecuHardware, @pecuType, @pengineProject, 
				@pdisplacement, @pemissionConcept, @pplatform, 
				@ppower, @pgearbox, @pvehicle, 
				@pmanufacturer);
  ELSE
    UPDATE obd.Flotte
    SET    protocol=@pprotocol, idSystem=@pidSystem, 
			ecuHardware=@pecuHardware, ecuType=@pecuType, engineProject=@pengineProject, 
			displacement=@pdisplacement, emissionConcept=@pemissionConcept, platform=@pplatform, 
			power=@ppower, gearbox=@pgearbox, vehicle=@pvehicle, 
			manufacturer=@pmanufacturer
    WHERE  idFlotte=@pidFlotte;
  END IF;
  
  COMMIT;
  pTransaccion := 1;
  pMensaje := 'Terminado OK';
  dbms_output.put_line('Terminado OK');

EXCEPTION
  WHEN OTHERS THEN
    pTransaccion := 0;
    pMensaje := 'Error en obd.Flotte_InsertarModificar:' || SQLCODE || ' Mensaje: ' || sqlerrm;
    dbms_output.put_line('Error en obd.Flotte_InsertarModificar:' || SQLCODE || ' Mensaje: ' || sqlerrm);
END;

PROCEDURE obd.FlotteConsultar(pNidDocumento NUMBER, pTransaccion OUT NUMBER, pCursor OUT cursor_type)
AS
BEGIN
  OPEN pCursor FOR
    SELECT idFlotte, protocol, idSystem, 
				ecuHardware, ecuType, engineProject, 
				displacement, emissionConcept, platform, 
				power, gearbox, vehicle, 
				manufacturer
    FROM   obd.Flotte
    WHERE  nidDocumento=pNidDocumento;
    
  pTransaccion := 1;
EXCEPTION 
  WHEN OTHERS THEN
    pTransaccion := 0;
END;

PROCEDURE obd.FlotteDetalleXML(pNidDocumento NUMBER, pTransaccion OUT NUMBER, pEncabezado OUT cursor_type,
                           pDatos OUT cursor_type, pIncidencias OUT cursor_type)
AS
BEGIN
  OPEN pEncabezado FOR
    SELECT d.nid, d.codigo, d.fechacreacion,
           (us.nombre ||' '||us.apellidopat||' '||us.apellidomat) as nombre, d.nidusuario
    FROM   documento d, usuarios us
    WHERE  d.nid=pnidDocumento
    AND    d.nidusuario=us.nid;
    
  OPEN pDatos FOR
    SELECT idFlotte, protocol, idSystem, 
				ecuHardware, ecuType, engineProject, 
				displacement, emissionConcept, platform, 
				power, gearbox, vehicle, 
				manufacturer
    FROM   obd.Flotte
    WHERE  nidDocumento=pNidDocumento;
    
  OPEN pIncidencias FOR
    SELECT i.fechaemision, i.descripcion, us.nombre
    FROM   incidencia i, usuarios us
    WHERE  niddocumento=pnidDocumento
    AND    i.nidusuario=us.nid;
    
    pTransaccion := 1;
    
EXCEPTION 
  WHEN OTHERS THEN
    pTransaccion := 0;
END;

--otros

END Pack_?Categoria_Flotte;
